package com.smh.a05FileDemo1;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.Arrays;

public class FileDemo7 {
    public static void main(String[] args) {

        //获取根盘符
        for (File file : File.listRoots()) {
            System.out.println(file);
        }
        System.out.println("--------------");
        //遍历获取文件名(包括后缀)
        File file = new File("day25-code\\src\\com\\smh\\File");
        String[] list = file.list();
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("--------------");
        //accept方法的形参，依次表示aaa文件夹里面每一个文件或者文件夹的路径
        //参数一:父级路径
        //参数二:子级路径
        //返回值:如果返回值为true，就表示当前路径保留
        //如果返回值为false，就表示当前路径会弃不要
        String[] list1 = file.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                File file1 = new File(dir, name);
                return file1.isFile() && name.endsWith(".txt");
            }
        });
        for (String s : list1) {
            System.out.println(s);
        }
        System.out.println("--------------");

        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isFile() && file1.getName().endsWith(".txt")) {
                System.out.println(file1.getName());
            }
        }

        System.out.println("--------------------");
        //这里是完整路径
        File[] files1 = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                //System.out.println(pathname);
                return pathname.isFile() && pathname.getName().endsWith(".txt");
            }
        });

        for (File file1 : files1) {
            System.out.println(file1);
        }
        System.out.println("--------------------");
        //这里是将路径拆开
        File[] files2 = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                File file1 = new File(dir, name);
                return file1.isFile() && name.endsWith(".txt");
            }
        });

        System.out.println(Arrays.toString(files2));
    }
}
